package ua.maxicash.util.criteria;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Restrictions;

import ua.maxicash.util.Filter;
import ua.maxicash.util.JqgridQuery;
import ua.maxicash.util.criteria.handlers.*;
import ua.maxicash.vo.Worker;

public class WorkerCriteriaChain implements CriteriaChain {
	  
		private List<CriteriaHandler> handlers;
	    protected List<String> aliases = new ArrayList<String>();
        private DetachedCriteria criteria = DetachedCriteria.forClass(Worker.class);
	    private Junction junction = null;

	    public WorkerCriteriaChain(){
	    }

	    /* (non-Javadoc)
		 * @see ua.maxicash.util.criteria.CriteriaChain#setUp(ua.maxicash.util.JqgridQuery)
		 */
	    @Override
		public void setUp(JqgridQuery jqgridQuery) {

	        handlers = new LinkedList<CriteriaHandler>();

	       if (jqgridQuery.getGroupOp().equals("AND")) {
            junction = Restrictions.conjunction();
            //criteria.add(Restrictions.conjunction());
        }

        if (jqgridQuery.getGroupOp().equals("OR")) {
            junction = Restrictions.disjunction();
            //criteria.add(Restrictions.disjunction());
        }

	        CriteriaHandler ch = null;

	        for (Filter filter : jqgridQuery.getRules()) {

	            if (filter.getField().equals("city")) {
	                ch = new CityCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }

	            if (filter.getField().equals("jobTypes")) {
	                ch = new JobTypesCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }

	            if (filter.getField().equals("region")) {
	                ch = new RegionCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }

	            if (filter.getField().equals("country")) {
	                ch = new CountryCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }
	            
	            if (filter.getField().equals("icq")) {
	                ch = new IcqCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }

	            if (filter.getField().equals("phone")) {
	                ch = new PhoneCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }
	            
	            if (filter.getField().equals("email")) {
	                ch = new EmailCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }
	            
	            if (filter.getField().equals("skype")) {
	                ch = new SkypeCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }
	            
	            if (filter.getField().equals("firstName")) {
	                ch = new FirstNameCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }
	            
	            if (filter.getField().equals("lastName")) {
	                ch = new LastNameCriteriaHandler(aliases);
	                ch.setUp(filter);
	                handlers.add(ch);
	            }

	        }


	    }

        @Override
	    public DetachedCriteria executeChain() {

	        for (CriteriaHandler handler : handlers) {
	            handler.handle(criteria,junction);
	        }

            criteria.add(junction);

	        return criteria;

	    }
}
